package LeetCode;

import java.util.HashSet;

public class LC_036_ValidSudoku {
    public static void main(String[] args) {

    }

    public boolean isValidSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            HashSet<Character> row = new HashSet<>();
            HashSet<Character> column = new HashSet<>();
            HashSet<Character> cube = new HashSet<>();
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.' && !row.add(board[i][j]))
                    return false;
                if (board[j][i] != '.' && !column.add(board[j][i]))
                    return false;
                int RowIndex = 3 * (i / 3) + j / 3;
                int ColIndex = 3 * (i % 3) + j % 3;
                if (board[RowIndex][ColIndex] != '.' && !cube.add(board[RowIndex][ColIndex]))
                    return false;
            }
        }
        return true;
    }

    public boolean isValidSudoku_2(char[][] board) {
        for (int i = 0; i < 9; i++) {
            int[] bit_map_row = new int[9];
            int[] bit_map_col = new int[9];
            int[] bit_map_cube = new int[9];

            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.')
                    if (bit_map_row[board[i][j] - '1'] == 1)
                        return false;
                    else
                        bit_map_row[board[i][j] - '1'] = 1;
                if (board[j][i] != '.')
                    if (bit_map_col[board[j][i] - '1'] == 1)
                        return false;
                    else
                        bit_map_col[board[j][i] - '1'] = 1;
                int RowIndex = 3 * (i / 3) + j / 3;
                int ColIndex = 3 * (i % 3) + j % 3;
                int val = board[RowIndex][ColIndex];
                if (val != '.')
                    if (bit_map_cube[val - '1'] == 1)
                        return false;
                    else
                        bit_map_cube[val - '1'] = 1;
            }
        }
        return true;
    }
}
